package com.cheer.service.impl;

import com.cheer.dto.MallCouponLoginDTO;
import com.cheer.dto.MallCouponUpdateDTO;
import com.cheer.entity.MallOrderEntity;
import com.cheer.entity.MallSalesroomEntity;
import com.cheer.enums.MallOrderStatusEnum;
import com.cheer.exception.ServiceException;
import com.cheer.service.MallCouponService;
import com.cheer.service.MallOrderService;
import com.cheer.service.MallSalesroomService;
import com.cheer.vo.MallCouponVO;
import com.cheer.vo.SimpleObjectVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author cheer
 */
@Service
public class MallCouponServiceImpl implements MallCouponService {

    @Autowired
    private MallOrderService mallOrderService;

    @Autowired
    private MallSalesroomService mallSalesroomService;

    @Override
    public List<SimpleObjectVO> listSalesroom(String number) {
        Long shopId = mallOrderService.getShopIdByVirNumber(number);
        if (shopId == null) {
            throw new ServiceException("电子券码对应的商户不存在");
        }

        List<MallSalesroomEntity> salesroomList = mallSalesroomService.listByShopId(shopId);

        return salesroomList.stream()
                .map(mallSalesroomEntity -> new SimpleObjectVO()
                        .setId(mallSalesroomEntity.getId())
                        .setName(mallSalesroomEntity.getName()))
                .collect(Collectors.toList());
    }

    @Override
    public MallCouponVO login(MallCouponLoginDTO couponLoginDTO) {
        MallSalesroomEntity salesroom = mallSalesroomService.getById(couponLoginDTO.getSalesroomId());
        if (salesroom == null) {
            throw new ServiceException("门店不存在");
        }

        if(!StringUtils.equals(couponLoginDTO.getWord(), salesroom.getWord())){
            throw new ServiceException("口令不正确");
        }

        MallOrderEntity order = mallOrderService.getByVirNumber(couponLoginDTO.getNumber());
        if (order == null) {
            throw new ServiceException("电子券码不存在");
        }

        return new MallCouponVO()
                .setMemberName(order.getReceiver())
                .setNumber(couponLoginDTO.getNumber())
                .setPhone(order.getPhone())
                .setProductName(order.getProductName())
                .setSalesroomId(salesroom.getId())
                .setSalesroomName(salesroom.getName());
    }

    @Override
    public void update(MallCouponUpdateDTO couponUpdateDTO) {
        MallOrderEntity order = mallOrderService.getByVirNumber(couponUpdateDTO.getNumber());
        if (order == null) {
            throw new ServiceException("电子券码不存在");
        }

        if (!Objects.equals(order.getStatus(), MallOrderStatusEnum.TO_DELIVER.getCode())) {
            throw new ServiceException("电子券码已被消费");
        }

        MallOrderEntity updateOrder = new MallOrderEntity()
                .setId(order.getId())
                .setStatus(MallOrderStatusEnum.FINISHED.getCode())
                .setHexiaoNumber(couponUpdateDTO.getHexiaoNumber())
                .setHexiaoTime(new Date())
                .setSalesroomId(couponUpdateDTO.getSalesroomId());
        mallOrderService.updateById(updateOrder);
    }
}
